/* 
 * File:   main.cpp
 * Author: notnarb
 *
 * Created on March 4, 2012, 6:08 PM
 */

#include <cstdlib>
#include "RBHash.h"
#include <string>
#include <sstream>

#define DEFAULT_DEBUG false
using namespace std;

/*
 * 
 */

bool g_debug = DEFAULT_DEBUG;
int main(int argc, char** argv) {

  RBHash* rbh = new RBHash;

  rbh->bulkLoad();
  /*
  rbh->preorder(64);
  g_debug = true;
  rbh->insert(500);
  rbh->insert(0);
  rbh->preorder(2);
  rbh->insert(-1);
  rbh->insert(-2);
  rbh->preorder(4);
  */

  int counter = 0;

  while(counter < 5){ //infinite loop
    cout << "Enter a command: ";
    string input;
    getline(cin,input);
    
    istringstream iss(input);
    string output;
    iss >> output;

    if (output == "insert")
      while( iss >> output)
	{
	  //	  cout << output << endl;
	  rbh->insert(atoi(output.c_str()));
	}
    else if (output == "preorder")
      {
	iss >> output;
	rbh->preorder(atoi(output.c_str()));
      }
    else if (output == "remove")
      {
	cout << "@@@Function not implemented\n";
      }
    else if (output == "lookup")
      {
	iss >> output;
	rbh->lookup(atoi(output.c_str()));
      }
    else if (output == "range")
      {
	iss >> output;
	int a = atoi(output.c_str());
	iss >> output;
	int b = atoi(output.c_str());
	rbh->range(a,b);
      }
    else if (output == "debug")
      g_debug = 1-g_debug;
    else if (output == "successor")
      {
	iss >> output;
	rbh->successor(atoi(output.c_str()));
      }
    else if (output == "predecessor")
      {
	iss >> output;
	rbh->predecessor(atoi(output.c_str()));
      }
    else if (output == "height")
      {
	iss >> output;
	rbh->height();
      }
    else if (output == "children")
      {
	iss >> output;
	rbh->children(atoi(output.c_str()));
      }
    else if (output == "exit")
      {
	return 0;
      }
    else if (output == "printHT" || output == "printht")
      {
	rbh->printHT();
      }
    else
      {
	cout << "Command \"" << output << "\" not recognized!\n";
	cout << "Recongized commands: insert, remove, lookup, range, successor, predecessor, height, children, preorder, printHT, printht, debug, exit\n";
	counter +=2;
      }
    counter--;

	
  }
    return 0;
}

